//
//  ASAvatarClassController.m
//  AvatarShow
//
//  Created by xunruiIOS on 2021/4/28.
//

#import "ASAvatarClassController.h"
#import "ASHomeApi.h"
#import "ASAvatarClassHomeModel.h"
#import "TJWaterfallLayout.h"
#import "ASAvatarClassHomeCell.h"
#import "ASAvatarClassListVC.h"
#import "ASAvatarClassGroupVC.h"

@interface ASAvatarClassController () <TJWaterfallLayoutDelegate,UICollectionViewDelegate,UICollectionViewDataSource>

@property (strong, nonatomic) NSMutableArray *dataArr;
@property (strong, nonatomic) UICollectionView *collectionView;

@end

@implementation ASAvatarClassController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithHexString:@"ffffff"];
    self.title = @"头像分类";
    [self.view addSubview:self.collectionView];
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsMake(0, 0, 0, 0));
    }];
    [self loadData];
    // Do any additional setup after loading the view.
}

- (void)loadData{
    NSArray *colorArr = @[@"#AB64A0",@"#888DCD",@"#E39591",@"#739CAE",@"#5C5A99",@"#AE9C8E",@"#498B99",@"#DFAE50"];
    [MBProgressHUD showMessage:@"加载中..."];
    [ASHomeApi loadAvatarClassDataCompletion:^(ASBaseModel *  _Nonnull result) {
        [MBProgressHUD hideHUD];
        if (result.state.intValue == 1) {
            [self.dataArr removeAllObjects];
            self.dataArr = [NSMutableArray new];
            for (int x = 0; x < result.resultArr.count; x++) {
                ASAvatarClassHomeModel *subM = result.resultArr[x];
                if ([subM.thumb isNotBlank]) {
                    int index = x;
                    if (colorArr.count < x) {
                        index = (int)colorArr.count - x;
                    }
                    subM.colorStr = colorArr[index];
                    [self.dataArr addObject:subM];
                }
            }
            [self.collectionView reloadData];
        }else{
            [MBProgressHUD showError:result.errorMsg];
        }
    } failure:^(NSError * _Nonnull error) {
        [MBProgressHUD hideHUD];
        [MBProgressHUD showError:FENETERROR];
    }];
}

- (UICollectionView *)collectionView{
    if (!_collectionView) {
        TJWaterfallLayout * waterFallLayout = [[TJWaterfallLayout alloc]init];
        waterFallLayout.delegate = self;
        waterFallLayout.sectionInset = UIEdgeInsetsMake(15, 14, 15, 14);
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:waterFallLayout];
        _collectionView.backgroundColor = self.view.backgroundColor;
        _collectionView.dataSource = self;
        _collectionView.delegate = self;
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.showsHorizontalScrollIndicator = NO;
        [_collectionView registerClass:[ASAvatarClassHomeCell class] forCellWithReuseIdentifier:ASAvatarClassHomeCellStr];

    }
    return _collectionView;
}

#pragma mark UICollectionViewDelegate
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataArr.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    ASAvatarClassHomeCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ASAvatarClassHomeCellStr forIndexPath:indexPath];
    if (self.dataArr.count > indexPath.row) {
        cell.model = self.dataArr[indexPath.row];
    }
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if (self.dataArr.count > indexPath.row) {
        ASAvatarClassHomeModel *model = self.dataArr[indexPath.row];
        
        [ASTool addUMTongJiKey:@"home" subKey:@"classification" titleStr:model.name];
        if ([model.tID isEqualToString:@"3774"]) {          ///套图页面
            ASAvatarClassGroupVC *groupVC = [ASAvatarClassGroupVC new];
            groupVC.subID = model.tID;
            groupVC.title = model.name;
            [self.navigationController pushViewController:groupVC animated:YES];
        }else{      ///分类页面
            ASAvatarClassListVC *groupVC = [ASAvatarClassListVC new];
            groupVC.subID = model.tID;
            groupVC.title = model.name;
            [self.navigationController pushViewController:groupVC animated:YES];
        }
    }
}

#pragma mark FJWaterfallFlowLayoutDelegate
//计算item高度的代理方法，将item的高度与indexPath传递给外界
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(TJWaterfallLayout *)collectionViewLayout heightForWidth:(CGFloat)width atIndexPath:(NSIndexPath *)indexPath{
    if (self.dataArr.count > indexPath.row) {
        ASAvatarClassHomeModel *model = self.dataArr[indexPath.row];
        return model.showHeight;
    }
    return CGFLOAT_MIN;
}

//区列数
- (NSInteger)collectionView:(UICollectionView *)collectionView layout:(TJWaterfallLayout*)collectionViewLayout colCountForSectionAtIndex:(NSInteger)section {
    return 2;
}

//行间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(TJWaterfallLayout*)collectionViewLayout lineSpacingForSectionAtIndex:(NSInteger)section {
    return 7;
}

//垂直间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(TJWaterfallLayout*)collectionViewLayout interitemSpacingForSectionAtIndex:(NSInteger)section {
    return 7;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
